/*******************************************************************************
Author:lonn
Date:2022-10-26
*******************************************************************************/

#include "menu.h"
#include "oled.h"
extern unsigned int adcx;
extern unsigned int current_duty;
extern float dis;
extern unsigned char RxData;
extern unsigned int change_flag;
extern char Change_Mode;
extern char lanya_flag;

extern char Change_Mode;

char function_Ldr=0;
char function_Voice=1;
char function_Csb=0;
char function_Irda=1;







void (*current_operation_index)();
uint8_t func_index;
//uint8_t refresh_flag=0;


key_table code_table[]={
 
			{0,7,1,0,(*fun1)},//界面1
			{1,0,2,1,(*fun2)},
			{2,1,3,2,(*fun3)},
			{3,2,4,3,(*fun4)},
			
			{4,3,5,6,(*fun5)},
			{5,4,7,5,(*fun6)},
			

			{6,6,6,4,(*fun7)},//fun5下层
			{7,5,0,7,(*fun8)},//蓝牙
			

};

//感光
void fun1(void)
{
	OLED_ShowCHinese(20+16*0,0,14); //感
	OLED_ShowCHinese(20+16*1,0,15); //光
	
	OLED_ShowCHinese(20+16*0,2,10); //语
	OLED_ShowCHinese(20+16*1,2,11); //音
	
	OLED_ShowCHinese(20+16*0,4,12); //超
	OLED_ShowCHinese(20+16*1,4,13); //声
	
	OLED_ShowCHinese(20+16*0,6,8); //红
	OLED_ShowCHinese(20+16*1,6,9); //外

	Kaiguan_flag();

	OLED_ShowCHinese( 0+16*0,0,6);//箭头
	
}
//语音
void fun2(void)
{
	OLED_ShowCHinese(20+16*0,0,14); //感
	OLED_ShowCHinese(20+16*1,0,15); //光
	
	OLED_ShowCHinese(20+16*0,2,10); //语
	OLED_ShowCHinese(20+16*1,2,11); //音
	
	OLED_ShowCHinese(20+16*0,4,12); //超
	OLED_ShowCHinese(20+16*1,4,13); //声
	
	OLED_ShowCHinese(20+16*0,6,8); //红
	OLED_ShowCHinese(20+16*1,6,9); //外
	
	
	Kaiguan_flag();

	OLED_ShowCHinese( 0+16*0,2,6);//箭头
	
}
//超声
void fun3(void)
{
	OLED_ShowCHinese(20+16*0,0,14); //感
	OLED_ShowCHinese(20+16*1,0,15); //光
	
	OLED_ShowCHinese(20+16*0,2,10); //语
	OLED_ShowCHinese(20+16*1,2,11); //音
	
	OLED_ShowCHinese(20+16*0,4,12); //超
	OLED_ShowCHinese(20+16*1,4,13); //声
	
	OLED_ShowCHinese(20+16*0,6,8); //红
	OLED_ShowCHinese(20+16*1,6,9); //外
	
	
	Kaiguan_flag();

	OLED_ShowCHinese( 0+16*0,4,6);//箭头
	
}
//红外
void fun4(void)
{
	OLED_ShowCHinese(20+16*0,0,14); //感
	OLED_ShowCHinese(20+16*1,0,15); //光
	
	OLED_ShowCHinese(20+16*0,2,10); //语
	OLED_ShowCHinese(20+16*1,2,11); //音
	
	OLED_ShowCHinese(20+16*0,4,12); //超
	OLED_ShowCHinese(20+16*1,4,13); //声
	
	OLED_ShowCHinese(20+16*0,6,8); //红
	OLED_ShowCHinese(20+16*1,6,9); //外
	
	
	Kaiguan_flag();

	OLED_ShowCHinese( 0+16*0,6,6);//箭头
	
}
//fun
void fun5(void)
{

	OLED_ShowString(20,0,(unsigned char *)"Function",16);
	
	OLED_ShowCHinese(20+16*0,2,20); //模
	OLED_ShowCHinese(20+16*1,2,21); //式
	
	OLED_ShowCHinese(20+16*0,4,30); //蓝
	OLED_ShowCHinese(20+16*1,4,31); //牙

	//Kaiguan_flag();
	Change_Mode_flag();

	OLED_ShowCHinese( 0+16*0,0,6);//箭头
}

//模式
void fun6(void)
{

	OLED_ShowString(20,0,(unsigned char *)"Function",16);
	OLED_ShowCHinese(20+16*0,2,20); //模
	OLED_ShowCHinese(20+16*1,2,21); //式
	
	OLED_ShowCHinese(20+16*0,4,30); //蓝
	OLED_ShowCHinese(20+16*1,4,31); //牙

	//Kaiguan_flag();
	Change_Mode_flag();

	OLED_ShowCHinese( 0+16*0,2,6);//箭头
}

//fun下层
void fun7(void)
{

	   OLED_ShowString(0,3,(unsigned char *)"Ldr:",8);
		OLED_ShowString(0,5,(unsigned char *)"CSB:",8);
	  OLED_ShowString(0,7,(unsigned char *)"DUTY :",8);
	  //OLED_ShowString(10,7,(unsigned char *)"MODE :",8);
		
			OLED_ShowNum(60,1,(int)Change_Mode/100,2,4);
		OLED_ShowNum(68,1,(int)Change_Mode/10%10,2,4);
		OLED_ShowNum(76,1,(int)Change_Mode%10,2,4);
	
		OLED_ShowNum(60,3,adcx/100,2,4);
		OLED_ShowNum(68,3,adcx/10%10,2,4);
		OLED_ShowNum(76,3,adcx%10,2,4);
	
		OLED_ShowNum(60,5,(int)dis/100,2,4); //百位
		OLED_ShowNum(68,5,(int)dis/10%10,2,4);//十位
		OLED_ShowNum(76,5,(int)dis%10,2,4);//个位	
	
		OLED_ShowNum(60,7,(current_duty)/100,2,4); //百位
		OLED_ShowNum(68,7,(current_duty)/10%10,2,4);//十位
		OLED_ShowNum(76,7,(current_duty)%10,2,4);//个位
		//OLED_ShowNum(60,7,1,2,4);//mode	 


	//Kaiguan_flag();

	//OLED_ShowCHinese( 0+16*0,0,6);//箭头
}

//蓝牙
void fun8(void)
{

	OLED_ShowString(20,0,(unsigned char *)"Function",16);
	
	OLED_ShowCHinese(20+16*0,2,20); //模
	OLED_ShowCHinese(20+16*1,2,21); //式
	
	OLED_ShowCHinese(20+16*0,4,30); //蓝
	OLED_ShowCHinese(20+16*1,4,31); //牙

	//Kaiguan_flag();
	Change_Mode_flag();

	OLED_ShowCHinese( 0+16*0,4,6);//箭头
}

	
void Kaiguan_flag(void){

		if(function_Ldr == 1)
				OLED_ShowCHinese(20+16*5,0,28);
		else if(function_Ldr == 0)
				OLED_ShowCHinese(20+16*5,0,29);
		
		if(function_Voice == 1)
				OLED_ShowCHinese(20+16*5,2,28);
		else if(function_Voice == 0)
				OLED_ShowCHinese(20+16*5,2,29);
		
		if(function_Csb == 1)
				OLED_ShowCHinese(20+16*5,4,28);
		else if(function_Csb == 0)
				OLED_ShowCHinese(20+16*5,4,29);
		
		if(function_Irda == 1)
				OLED_ShowCHinese(20+16*5,6,28);
		else if(function_Irda == 0)
				OLED_ShowCHinese(20+16*5,6,29);

}

void Change_Mode_flag(void){

		if(Change_Mode == 1)
		{
				OLED_ShowCHinese(20+16*4,2,18);/*"护",18*/
				OLED_ShowCHinese(20+16*5,2,19);/*"眼",19*/
		}
		else if(Change_Mode == 2)
		{
				OLED_ShowCHinese(20+16*4,2,16);/*"明",16*/
				OLED_ShowCHinese(20+16*5,2,17);/*"亮",17*/
		}
		
		if(lanya_flag == 0)
		{
				OLED_ShowCHinese(20+16*3,4,32);/*"已",32*/
				OLED_ShowCHinese(20+16*4,4,35);/*"断",35*/
				OLED_ShowCHinese(20+16*5,4,36);/*"开",36*/
		}
		else if(lanya_flag == 1)
		{
				OLED_ShowCHinese(20+16*3,4,32);/*"已",32*/
				OLED_ShowCHinese(20+16*4,4,33);/*"连",33*/
				OLED_ShowCHinese(20+16*5,4,34);/*"接",34*/
		}


}

void Menu_key_set(char Menu_key){
		
    if (Menu_key == 1)
    {
        func_index = code_table[func_index].up;
			OLED_Clear(0);
    }
    
		if (Menu_key == 2)
    {
        func_index = code_table[func_index].down;
			OLED_Clear(0);
    }
		
		if (Menu_key == 5)
    {
        OLED_Clear(0);
        func_index = code_table[func_index].enter;
			
				if (func_index == 0)
						if(function_Ldr == 1)
							function_Ldr = 0;
						else function_Ldr = 1;
				
				else if (func_index == 1)

						if(function_Voice == 1){
							function_Voice = 0;
								GPIO_ResetBits(GPIOC,GPIO_Pin_13);}
						else {function_Voice = 1;
								GPIO_SetBits(GPIOC,GPIO_Pin_13);}
				
				else if (func_index == 2)

						if(function_Csb == 1)
							function_Csb = 0;
						else function_Csb = 1;
				
				else if (func_index == 3)

						if(function_Irda == 1)
							function_Irda = 0;
						else function_Irda = 1;
						
				else if (func_index == 5)
				
						if(Change_Mode == 1)
							Change_Mode = 2;
						else Change_Mode = 1;
    }

    current_operation_index = code_table[func_index].current_operation;
    (*current_operation_index)(); //执行当前操作函数
		
}

void Menu_key_fanye(char Menu_fanye){
	//上翻页
	if(Menu_fanye == 2){
		if(func_index/4>MIN_PAGE-1)
		{
			func_index = ((func_index/4)*4)-4;
		}
		else func_index = (MIX_PAGE*4)-4;
	
		OLED_Clear(0);
	}
		//下翻页
	if(Menu_fanye == 1){
		if(func_index/4<MIX_PAGE-1)
		{
			func_index = ((func_index/4)*4)+4;
		}
		else func_index = (MIN_PAGE*4)-4;
	
	
		OLED_Clear(0);
	}

    current_operation_index = code_table[func_index].current_operation;
    (*current_operation_index)(); //执行当前操作函数
		
}
